package models

import "gorm.io/gorm"

type UserModel struct {
	gorm.Model
	UserId   int64  `json:"user_id" gorm:"index:user_id;"`
	Phone    string `json:"phone" gorm:"type:varchar(11);unique_index"`
	PassWord string `json:"password" gorm:"type:varchar(255)"`
	Status   int    `json:"status" gorm:"type:int(1)"` // 0:正常 1:禁用
	Ext      string `json:"ext" gorm:"type:varchar(255)"`
}

// Register register user
func (u UserModel) Register(db *gorm.DB, user *UserModel) int {
	db.Create(user).Take(&user)
	return int(user.UserId)
}

// GetUserInfoByPhone get user info by phone
func (u UserModel) GetUserInfoByPhone(db *gorm.DB, phone string) (user UserModel, err error) {
	err = db.Model(UserModel{}).Where("phone = ?", phone).First(&user).Error
	return
}

// ForbidUser forbid user
func (u UserModel) ForbidUser(db *gorm.DB, userId int64) {
	db.Model(UserModel{}).Where("user_id = ?", userId).Update("status", 1)
}
